package 字母;

import java.util.Scanner;

public class Main {
    static int N = 30, n, m;
    static char[][] g = new char[N][N];
    static boolean[] st = new boolean[N];
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        for (int i = 0; i < n; i++) {
            String s = in.next();
            for (int j = 0; j < m; j++) {
                g[i][j] = s.charAt(j);
            }
        }
        System.out.println(dfs(0, 0));
    }

    private static int dfs(int x, int y) {
        int u = g[x][y] - 'A';
        st[u] = true;
        int sum = 0;
        for (int i = 0; i < 4; i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= m) {
                continue;
            }
            int t = g[a][b] - 'A';
            if (!st[t]) {
                sum = Math.max(sum, dfs(a, b));
            }
        }
        st[u] = false;
        return sum + 1;
    }
}
